﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_ep_question_type_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            ddlepType.DataSource = dal.ePioneer_QuestionType;
            ddlepType.DataTextField = "name";
            ddlepType.DataValueField = "id";
            ddlepType.DataBind();
            ddlepType.Items.Insert(0, new ListItem("请选择", "0"));
            if (Request["id"] != null)
            {
                HiddenFieldType.Value = Request["id"];
                int id = int.Parse(Request["id"].ToString());

                CMSModel.ePioneer_QuestionType m_epc = dal.ePioneer_QuestionType.First(x => x.id == id);

                ddlepType.SelectedValue = m_epc.pid.ToString();
                txt_brief.Text = m_epc.name;
            }
        }

    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnModify_Click(object sender, EventArgs e)
    {
        if (HiddenFieldType.Value != "")
        {
            try
            {
                int id = int.Parse(HiddenFieldType.Value);
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                CMSModel.ePioneer_QuestionType m_epc = dal.ePioneer_QuestionType.First(x => x.id == id);
                m_epc.name = txt_brief.Text;
                m_epc.pid = int.Parse(ddlepType.SelectedValue);
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='ep_question_type.aspx'", true);
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改失败');", true);
            }
        }
        else
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            CMSModel.ePioneer_QuestionType m_epc = new CMSModel.ePioneer_QuestionType();
            m_epc.name = txt_brief.Text;
            m_epc.pid = int.Parse(ddlepType.SelectedValue);
            try
            {
                dal.AddToePioneer_QuestionType(m_epc);
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='ep_question_type.aspx'", true);
            }
            catch (Exception)
            {

                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败');", true);
            }
        }
    }
}